package com.etoak.common.redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class RedisService {

    // 注入redisTemplate
    @Autowired
    StringRedisTemplate stringRedisTemplate;

    // 添加数据
    public void setex(String key, String value, long expire, TimeUnit timeUnit){
        stringRedisTemplate.opsForValue().set(key, value, expire, timeUnit);
    }
    // 获取数据
    public String get(String key){
        return stringRedisTemplate.opsForValue().get(key);
    }

    /**
     * Hash hset
     */
    public void hest(String key,String field,String value){
        stringRedisTemplate.opsForHash().put(key,field,value);
    }

    /**
     * Hash hvals
     */
    public List<String> hvals(String key){
        return stringRedisTemplate.opsForHash().values(key)
                .stream().map(Object::toString).toList();
    }

    /**
     * Hash hdel
     */
    public void hdel(String key,String... fields){
        stringRedisTemplate.opsForHash().delete(key,fields);
    }

    /**
     * 全局del
     */
    public void del(String key){
        stringRedisTemplate.delete(key);
    }


}
